package j20251013;

import java.io.*;
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入源文件：");
        String srcPath=sc.next();
        File srcFile=new File(srcPath);
        if(!srcFile.isFile()){
            System.out.println("输入的不是文件或不存在！");
            return;
        }
        System.out.println("请输入目标文件路径：");
        String destPath=sc.next();
        File destFile=new File(destPath);
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("输入的目标文件所在目录不存在！");
            return;
        }

        //进行文件的复制
        try (InputStream inputStream=new FileInputStream(srcFile);
             OutputStream outputStream=new FileOutputStream(destFile)){
            while(true){
                byte[] buf=new byte[1024];
                int read=inputStream.read(buf);
                if(read==-1){
                    break;
                }
                outputStream.write(buf,0,read);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
